package zhihui.mobile.presenter.shop;

import android.content.Context;

import org.json.JSONObject;

import zhihui.mobile.bean.shop.address.ShoppingAddress;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.address.AddressManager;
import zhihui.mobile.manager.shop.ShopAddressManager;
import zhihui.mobile.task.AreaLoadTask;
import zhihui.mobile.ui.IView;
import zhihui.mobile.ui.shop.address.IShopAddressAddContract;

/**
 * Created by 张公铭 on 2017/8/16 0016.
 */

public class ShopAddressAddPresenter implements IShopAddressAddContract.IShopAddressAddPresenter
{
    private Context mContext;
    private IShopAddressAddContract.IShopAddressAddView addView;
    private ShoppingAddress curAddress = new ShoppingAddress();
    private int updateId = 0;

    @Override
    public void init(IView view, Context context)
    {
        mContext = context;
        addView = (IShopAddressAddContract.IShopAddressAddView) view;
    }

    @Override
    public void onResume()
    {
        if (updateId != 0)
        {
            for (ShoppingAddress address : ShopAddressManager.getInstance().getmList())
            {
                if (address.getId() == updateId)
                {
                    addView.initData(address);
                    break;
                }
            }
        }
    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void showAddressDialog()
    {
        if (AddressManager.getInstance().getmAddressList() == null || AddressManager.getInstance().getmAddressList().size() == 0)
        {
            addView.showLoading();
            new AreaLoadTask(mContext, new AreaLoadTask.ICallBackListener()
            {
                @Override
                public void finish(boolean result)
                {
                    if (result)
                    {
                        addView.hindLoading();
                        addView.showAddressDialog(AddressManager.getInstance().getmAddressList());
                    }
                }
            }).execute();
        } else
        {
            addView.showAddressDialog(AddressManager.getInstance().getmAddressList());
        }
    }

    @Override
    public void setProv(String name, String code)
    {
        curAddress.setProv(name);
        curAddress.setProvcode(code);
    }

    @Override
    public void setCity(String name, String code)
    {
        curAddress.setCity(name);
        curAddress.setCitycode(code);
    }

    @Override
    public void setArea(String name, String code)
    {
        curAddress.setDist(name);
        curAddress.setDistcode(code);
    }

    @Override
    public void addAddress(String name, String phone, final String address)
    {
        curAddress.setName(name);
        curAddress.setTel(phone);
        curAddress.setDetails(address);

        addView.showLoading();
        ShopAddressManager.getInstance().addAddress(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                addView.hindLoading();
                addView.showMessage("添加成功！");
                ShopAddressManager.getInstance().getmList().add(curAddress);
                addView.goBack(mContext.getClass());
            }

            @Override
            public void failed(String message)
            {
                addView.hindLoading();
                addView.showMessage(message);
            }
        }, curAddress);
    }

    @Override
    public void setUpdateId(int addressId)
    {
        updateId = addressId;
    }
}
